package com.schoolMall.service.impl;

import com.schoolMall.dao.LocalAuthDao;
import com.schoolMall.domain.LocalAuth;
import com.schoolMall.dto.LocalAuthDto;
import com.schoolMall.enums.LocalAuthStateEnum;
import com.schoolMall.exception.LocalAuthOperationException;
import com.schoolMall.service.LocalAuthService;
import com.schoolMall.utils.MD5;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * @author:zyh
 * @Time:2021-09-04-15:04
 * @email:1269231889@qq.com
 */
@Service
public class LocalAuthServiceImpl implements LocalAuthService {
    @Autowired
    private LocalAuthDao localAuthDao;
    private Logger logger = LoggerFactory.getLogger(LocalAuthServiceImpl.class);

    @Override
    public LocalAuth getLocalAuthByUsernameAndPwd(String username, String password) {
        if (Objects.isNull(username) || Objects.isNull(password)) {
            logger.info("user is null or password is null");
            return null;
        }
        return localAuthDao.queryLocalByUserNameAndPwd(username, MD5.getMd5(password));
    }

    @Override
    public LocalAuth getLocalAuthByUserId(long userId) {
        return localAuthDao.queryLocalByUserId(userId);
    }

    @Override
    @Transactional
    public LocalAuthDto bindLocalAuth(LocalAuth localAuth) throws LocalAuthOperationException {
        if (Objects.isNull(localAuth) || Objects.isNull(localAuth.getUser()) ||
                Objects.isNull(localAuth.getUsername()) || Objects.isNull(localAuth.getPassword()) || Objects.isNull(localAuth.getUser().getUserId())) {
            logger.info("传入的有些参数为空");
            return new LocalAuthDto(LocalAuthStateEnum.NULL_USER);
        }
        LocalAuth temp = localAuthDao.queryLocalByUserId(localAuth.getUser().getUserId());
        if (Objects.nonNull(temp)) {
            return new LocalAuthDto(LocalAuthStateEnum.ONLY_ONE_USER);
        }
        localAuth.setCreateTime(new Date());
        localAuth.setLastEditTime(new Date());
        localAuth.setPassword(MD5.getMd5(localAuth.getPassword()));
        int effectNum = localAuthDao.insertLocalAuth(localAuth);
        if (effectNum < 1) {
            throw new LocalAuthOperationException("绑定失败");
        }
        return new LocalAuthDto(LocalAuthStateEnum.SUCCESS, localAuth);
    }

    @Override
    @Transactional
    public LocalAuthDto modifyLocalAuth(Long userId, String username, String password, String newPassword) throws LocalAuthOperationException {
        if (Objects.isNull(userId) || Objects.isNull(username) || Objects.isNull(password) ||
                Objects.isNull(newPassword) || !password.equals(newPassword)) {
            return new LocalAuthDto(LocalAuthStateEnum.NULL_USER);
        }
        int effectNum = localAuthDao.updateLocalAuth(userId, username, password, newPassword, new Date());
        if (effectNum < 1) {
            throw new LocalAuthOperationException("更新失败");
        }
        return new LocalAuthDto(LocalAuthStateEnum.SUCCESS);
    }


}
